package TUILayer;//makes this class a part of the TUILayer package
import java.util.Scanner;
import java.util.Iterator;
import ControlLayer.*; //The TUILayer needs to know the ControlLayer
import ModelLayer.*;

/**
 * User interface class to the AddressBook
 * 
 * @author (group1) 
 * @version (2013.11.04)
 * (FEN): 2006.03.21: Comments translated into English
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 */
public class AddressBookUI extends InputOutput
{   

    private AddressCtr addCtr;  //reference to the ControlLayer

    public AddressBookUI() {   addCtr = AddressCtr.getInstance(); }

    public void start()    {   addressMenu();      }

    public void addressMenu()
    {
        boolean exit = false;
        while(!exit)
        {
            int choice = writeAddressBookMenu(); 
            System.out.println("\f");
            switch(choice)
            {
                case 1 :  getPerson();    break;   // Show Person Info
                case 2 :  createPerson(); break;   // Create Person
                case 3 :  deletePerson(); break;   // Delete Person
                case 4 :  updatePerson(); break;   // Update Person's Details
                case 5 :  listPeople();   break;   // Print All People
                default : exit=true;      break;   // exit
            }
        }
    }

    private void getPerson()
    {
        long id=inputLong("Id");
        Person tempPerson = addCtr.getPerson(id);
        if(tempPerson==null)   
            System.out.println(" The ID is incorrect. Sorry.");
        else    
            printPersonDetails(tempPerson);
    }

    private void createPerson()
    {
        long id=inputLong("Id");
        String name=inputString("Name");
        String address=inputString("Address");
        String postalCode=inputString("Postal Code");
        String city=inputString("City");
        String phone=inputString("Phone");

        addCtr.createPerson(id,name,address,postalCode,city,phone);
        System.out.println(" --> "+name+"\'s account was successfully created!");
    }

    private void deletePerson()
    {
        long id=inputLong("Id");
        if(addCtr.deletePerson(id))
            System.out.println(" --> The person with ID "+id+" was successfully deleted.");
        else
            System.out.println(" --> The person with ID "+id+" does not exist in the addressbook.");

    }

    private void updatePerson()
    {
        long id = inputLong("Id");
        int paramChoice=updatePersonMenu(id);
        if(id>=6) return;
        String updateValue = inputString("the value");
        if(addCtr.updatePerson(id,paramChoice,updateValue))
            System.out.println(" --> The person with ID "+id+" was successfully updated !");
        else
            System.out.println(" --> An error occurred while updating the person with ID "+id);
    }

    private void listPeople()
    {
        Iterator it=addCtr.listAllPersons();
        
        if(it.hasNext()==false)
        {
            System.out.println(" --> There is no person added.");
            return;
        }
        
        System.out.println(" ############### List of people #################");
        while (it.hasNext()) 
        {
            printPersonDetails((Person)it.next());
        }
    }

    public void printPersonDetails(Person person)
    {
        if(person==null) return;

        System.out.println("\n Id: " + person.getId());
        System.out.println(" Name: " + person.getName());
        System.out.println(" Address: " + person.getAddress());
        System.out.println(" Postal Code: " + person.getPostalCode());
        System.out.println(" City: " + person.getCity());
        System.out.println(" Phone: " + person.getPhone());
    }

    public int writeAddressBookMenu()
    { 
        System.out.println("\n *** AddressBook Menu ***");
        System.out.println(" (1) Show information about person");
        System.out.println(" (2) Add person");
        System.out.println(" (3) Delete person");
        System.out.println(" (4) Update person");
        System.out.println(" (5) List all persons");
        System.out.println(" (6) Return");
        System.out.print("\n Make your choice: ");

        return readInt();
    }

    public int updatePersonMenu(long id)
    { 
        System.out.println("\n\n *** Update Options for ID: "+id+" ***");
        System.out.println(" (1) name");
        System.out.println(" (2) address");
        System.out.println(" (3) postal code");
        System.out.println(" (4) city");
        System.out.println(" (5) phone");
        System.out.println(" (6) Return");
        System.out.print("\n Make your choice: ");

        return readInt();
    } 

}
